package com.itheima.reggie.service.impl;

import java.math.BigDecimal;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.*;
import com.itheima.reggie.service.OrdersService;
import com.itheima.reggie.utils.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
@Slf4j
public class OrdersServiceImpl implements OrdersService {
    @Autowired(required = false)
    private OrdersMapper ordersMapper;
    @Autowired(required = false)
    private OrderDetailMapper orderDetailMapper;
    @Autowired(required = false)
    private AddressBookMapper addressBookMapper;
    @Autowired(required = false)
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired(required = false)
    private UserMapper userMapper;

    @Override
    public void submit(Orders orders) {
        Long userId = orders.getUserId();
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(userId);
        User user = userMapper.findById(userId);
        List<OrderDetail> orderDetails = new ArrayList<>();
        AddressBook addressBook = addressBookMapper.getById(orders.getAddressBookId());
        Long orderId = UUIDUtils.getUUIDInOrderId().longValue();
        BigDecimal totalAmount = new BigDecimal("0");
        for (ShoppingCart shoppingCart : shoppingCartList) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setId(UUIDUtils.getUUIDInOrderId().longValue());
            orderDetail.setName(shoppingCart.getName());
            orderDetail.setOrderId(orderId);
            orderDetail.setDishId(shoppingCart.getDishId());
            orderDetail.setSetmealId(shoppingCart.getSetmealId());
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
            orderDetail.setNumber(shoppingCart.getNumber());
            orderDetail.setAmount(shoppingCart.getAmount());
            orderDetail.setImage(shoppingCart.getImage());
            BigDecimal amount = shoppingCart.getAmount().multiply(new BigDecimal(shoppingCart.getNumber()));
            totalAmount = totalAmount.add(amount);
            orderDetails.add(orderDetail);
        }
        orders.setId(orderId);
        orders.setNumber(orderId.toString());
        orders.setStatus(1);
        orders.setAddressBookId(addressBook.getId());
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setAmount(totalAmount);
        orders.setUserName(user.getName());
        orders.setPhone(user.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setConsignee(addressBook.getConsignee());
        ordersMapper.save(orders);
        orderDetailMapper.save(orderDetails);

        shoppingCartMapper.clean(userId);


    }

    @Override
    public Page<OrdersDto> userPage(int page, int pageSize) {
        PageHelper.startPage(page, pageSize);

        List<Orders> ordersList = ordersMapper.userPage(page, pageSize);
        PageInfo<Orders> ordersPageInfo = new PageInfo<>(ordersList);
//        System.out.println("ordersPageInfo.getTotal() = " + ordersPageInfo.getTotal());
        List<OrdersDto> collect = ordersPageInfo.getList().stream().map(orders -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(orders, ordersDto);
            List<OrderDetail> orderDetails=orderDetailMapper.findByIdOrder(ordersDto.getId());
            ordersDto.setOrderDetails(orderDetails);
            return ordersDto;
        }).collect(Collectors.toList());
        Page<OrdersDto> ordersPage = new Page<>(collect, ordersPageInfo.getTotal(), page, pageSize,ordersPageInfo.getPages());

        return ordersPage;
    }

    @Override
    public Page<OrdersDto> page(int page, int pageSize, String number, String beginTime, String endTime) {
        PageHelper.startPage(page, pageSize);
        List<Orders> ordersList = ordersMapper.page(page, pageSize, number, beginTime, endTime);
        PageInfo<Orders> ordersPageInfo = new PageInfo<>(ordersList);
        List<OrdersDto> collect = ordersPageInfo.getList().stream().map(orders -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(orders, ordersDto);
            User user = userMapper.findById(ordersDto.getUserId());
            ordersDto.setUserName(user.getName());
            ordersDto.setPhone(user.getPhone());
            AddressBook addressBook = addressBookMapper.getById(ordersDto.getAddressBookId());
            ordersDto.setAddress(addressBook.getDetail());
            ordersDto.setConsignee(addressBook.getConsignee());
            List<OrderDetail> orderDetails = orderDetailMapper.findByIdOrder(ordersDto.getId());
            ordersDto.setOrderDetails(orderDetails);
            return ordersDto;
        }).collect(Collectors.toList());
//        List<Orders> collect = ordersPageInfo.getList().stream().map(orders -> {
//            User user = userMapper.findById(orders.getUserId());
//            orders.setUserName(user.getName());
//            orders.setPhone(user.getPhone());
//            AddressBook addressBook = addressBookMapper.getById(orders.getAddressBookId());
//            orders.setAddress(addressBook.getDetail());
//            orders.setConsignee(addressBook.getConsignee());
//            return orders;
//        }).collect(Collectors.toList());
        Page<OrdersDto> ordersDtoPage = new Page<>(collect, ordersPageInfo.getTotal(), page, pageSize);
        return ordersDtoPage;
    }

    @Override
    public List<OrdersDto> find() {
        List<Orders> ordersList = ordersMapper.find();
        List<OrdersDto> collect = ordersList.stream().map(orders -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(orders, ordersDto);
            return ordersDto;

        }).collect(Collectors.toList());

        return collect;
    }
}
